package com.whr.service;

import com.whr.bean.User;
import com.whr.dao.ProblemMapper;
import com.whr.dao.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProblemMapper problemMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public void saveUser(User user) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = dateFormat.format(new Date());
        user.setDate(date);
        user.setPassword("123");
        user.setState("启用");
        userMapper.insert(user);
    }

    @Override
    public void stop(Integer[] checks) {
        userMapper.stopUser(checks);
    }

    @Override
    public void start(Integer[] checks) {
        userMapper.startUser(checks);
    }

    @Override
    public User findById(Integer checks) {
        return userMapper.selectByPrimaryKey(checks);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void delUser(Integer[] checks) {
        userMapper.delUser(checks);
    }

    @Override
    public void delParm(String parm) {
        userMapper.delParm(parm);
    }

    @Override
    public void rese(Integer[] checks) {
        userMapper.updatePass(checks);
    }

    @Override
    public void updateRole(String role, Integer uid) {
        userMapper.updateRole(role, uid);
    }

    @Override
    public String genProb(List<String> probs) {
        StringBuilder s = new StringBuilder();
        probs.add("   ");
        int i = 0;
        for (String prob : probs) {
            String bcolor = "#FFF";
            if (i % 2 == 0) {
                bcolor = "#F2F2F2";
            }
            s.append("<div style=\"width: 100%;height:35px;border-top:1px solid;border-color:#cecece;" +
                    "background-color: " + bcolor + ";line-height:35px\"><span style=\"margin-left:10px\">" + prob + "</span></div>");
            i++;
        }
        return s.toString();
    }

    @Override
    public String genFine(List<String> fines) {
        StringBuilder s = new StringBuilder();
        fines.add("   ");
        int i = 0;
        for (String prob : fines) {
            String bcolor = "#FFF";
            if (i % 2 == 0) {
                bcolor = "#F2F2F2";
            }
            s.append("<div style=\"width: 100%;height:35px;border-top:1px solid;border-color:#cecece;" +
                    "background-color: " + bcolor + ";line-height:35px\"><span style=\"margin-left:10px\">" + prob + "</span></div>");
            i++;
        }
        return s.toString();
    }

    @Override
    public String genReply(List<String> replys) {
        StringBuilder s = new StringBuilder();
        replys.add("   ");
        int i = 0;
        for (String prob : replys) {
            String bcolor = "#FFF";
            if (i % 2 == 0) {
                bcolor = "#F2F2F2";
            }
            s.append("<div style=\"width: 100%;height:35px;border-top:1px solid;border-color:#cecece;" +
                    "background-color: " + bcolor + ";line-height:35px\"><span style=\"margin-left:10px\">" + prob + "</span></div>");
            i++;
        }
        return s.toString();
    }

    @Override
    public String genRank() {
        Integer ynum = problemMapper.getYreplay();
        Integer znum = problemMapper.getZreplay();
        String y = "<tr><td><span>院系部</span></td><td><span>" + ynum + "</span></td></tr>\n" +
                "        <tr><td><span>职能部门</span></td><td><span>" + znum + "</span></td></tr>";
        String z = "<tr><td><span>职能部门</span></td><td><span>" + znum + "</span></td></tr>\n" +
                "        <tr><td><span>院系部</span></td><td><span>" + ynum + "</span></td></tr>";
        String rank = "";
        if (ynum > znum) {
            rank = y;
        } else {
            rank = z;
        }
        String s = "<style>\n" +
                "        table {border: 1px;border-collapse: collapse}\n" +
                "        table td,table th{border:1px solid #cecece;height: 37px;font-size: 14px}\n" +
                "        table tr:nth-child(odd){background: #f2f2f2}\n" +
                "        span {margin-left: 10px}\n" +
                "    </style>\n" +
                "    <table width=\"100%\">\n" +
                "        <tr><th align=\"left\" width=\"50%\"><span>部门</span></th><th align=\"left\" width=\"50%\"><span>回答数量</span></th></tr>\n" +
                rank +
                "        <tr><td><span>&nbsp;</span></td><td><span></span></td></tr>\n" +
                "    </table>";
        return s;
    }

    @Override
    public String genCount() {
        Integer ingCount = problemMapper.findIng();
        Integer repCount = problemMapper.findRepc();
        Integer endCount = problemMapper.findEnd();
        Integer overCount = problemMapper.findOverc();
        String s = "<style>\n" +
                "        .data {border-bottom: 1px dotted #cecece;font-size: 14px;clear: both;}\n" +
                "        .da {font-size: 14px;float: left;margin: 9px 10px 9px 10px;}\n" +
                "        .ta {float: left;margin-left: 13px;}\n" +
                "        img {width: 45px;height: 45px;margin: 7px auto 7px;float: left;}\n" +
                "    </style>\n" +
                "\t<div style=\"margin: 10px 22% 10px 22%;border: 1px dotted #cecece;\">\n" +
                "\t\t\t<div class=\"data\" style=\"border-bottom: 0px\"\n" +
                "            <div class=\"data\">\n" +
                "                <div class=\"ta\"><img src=\"/icon/orbino_icon_pack_001.png\"></div>\n" +
                "                <div class=\"da\">\n" +
                "                    <div style=\"font-size: 20px;color: #0b63af\">"+ingCount+"</div>\n" +
                "                    进行中问题\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div class=\"data\">\n" +
                "                <div class=\"ta\"><img src=\"/icon/orbino_icon_pack_008.png\"></div>\n" +
                "                <div class=\"da\">\n" +
                "                    <div style=\"font-size: 20px;color: #c0bb17\">"+repCount+"</div>\n" +
                "                    已回复问题\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div class=\"data\">\n" +
                "                <div class=\"ta\"><img src=\"/icon/orbino_icon_pack_007.png\"></div>\n" +
                "                <div class=\"da\">\n" +
                "                    <div style=\"font-size: 20px;color: #24ab16\">"+endCount+"</div>\n" +
                "                    已结束问题\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div class=\"data\">\n" +
                "                <div class=\"ta\"><img src=\"/icon/orbino_icon_pack_004.png\"></div>\n" +
                "                <div class=\"da\">\n" +
                "                    <div style=\"font-size: 20px;color: #a82324\">"+overCount+"</div>\n" +
                "                    已逾期问题\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div class=\"data\" style=\"border-bottom: 0px\"></div>\n" +
                "\t</div>";
        return s;
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public void upPass(String npass, Integer uid) {
        userMapper.uppass(npass,uid);
    }
}